package com.cf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cf.domain.ResponseResult;
import com.cf.domain.pojo.Certificate;
import com.cf.domain.pojo.News;
import com.cf.service.ICertificateService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/certs")
@RequiredArgsConstructor
public class CertificateController {

    private final ICertificateService certificateService;

    @RequestMapping("/certsUI")
    public String newsUI(){
        return "admin/cert";
    }

    /**
     * 添加
     */
    @PostMapping
    @ResponseBody
    public ResponseResult save(@RequestBody Certificate certificate){
        certificateService.save(certificate);
        return ResponseResult.sucess();
    }

    @PutMapping
    @ResponseBody
    public ResponseResult update(@RequestBody Certificate certificate){
        certificateService.updateById(certificate);
        return ResponseResult.sucess();
    }

    @GetMapping("/{id}")
    @ResponseBody
    public ResponseResult getById(@PathVariable Long id){
        Certificate certificate = certificateService.getById(id);
        return ResponseResult.sucess(certificate);
    }

    @GetMapping("/pagination")
    @ResponseBody
    public ResponseResult page(@RequestParam(defaultValue = "0") Integer pageNo,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam(defaultValue = "") String content){
        Map<String, Object> map = new HashMap<>();
        map.put("content", content);
        Page<Certificate> data = certificateService.page(new Page<Certificate>(pageNo, pageSize), map);
        return ResponseResult.sucess(data);
    }

    @GetMapping("/search")
    @ResponseBody
    public ResponseResult index(@RequestParam(defaultValue = "") String content){
        Map<String, Object> map = new HashMap<>();
        map.put("content", content);
        List<Certificate> data = certificateService.search(map);
        return ResponseResult.sucess(data);
    }
}
